package com.zhanghe.study.springcloudalibaba.service;

import com.zhanghe.study.springcloudalibaba.client.AccountClient;
import com.zhanghe.study.springcloudalibaba.client.StorageClient;
import com.zhanghe.study.springcloudalibaba.dao.OrderDao;
import com.zhanghe.study.springcloudalibaba.model.Order;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zh
 * @date 2021/9/15 15:18
 */
@Service
public class OrderService {
    @Autowired
    private OrderDao orderDao;

    @Autowired
    private StorageClient storageClient;

    @Autowired
    private AccountClient accountClient;

    // 创建订单
    @GlobalTransactional(name = "create-order",rollbackFor = Exception.class)
    public void create(Order order){
        System.out.println("创建订单");
        orderDao.addOrder(order);

        System.out.println("扣减库存");
        storageClient.decrease(order.getProductId(),order.getCount());

        System.out.println("账户余额扣减");
        accountClient.decrease(order.getUserId(),order.getMoney());

        System.out.println("订单状态修改");
        orderDao.updateStatus(order.getId(),order.getStatus());
    }


}
